package com.jinxiangyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinxiangyang.domain.CouponInfo;
import com.jinxiangyang.mapper.CouponInfoMapper;
import com.jinxiangyang.service.CouponInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jinxiangyang.utils.DateUtils;
import com.jinxiangyang.utils.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 优惠券信息 服务实现类
 * </p>
 *
 * @author jinxiangyang
 * @since 2023-08-12
 */
@Service
public class CouponInfoServiceImpl extends ServiceImpl<CouponInfoMapper, CouponInfo> implements CouponInfoService {

    @Autowired
    CouponInfoMapper couponInfoMapper;

    @Override
    public AjaxResult findCouponByPage(Integer currentPage, Integer pageSize) {
        Page<CouponInfo> page = new Page<>(currentPage,pageSize);
        return AjaxResult.success(couponInfoMapper.selectPage(page,null));
    }

    @Override
    public boolean saveCouponInfo(CouponInfo couponInfo) {
        Date nowDate = DateUtils.getNowDate();
        couponInfo.setCreateTime(nowDate);
        couponInfo.setUpdateTime(nowDate);
        int i = couponInfoMapper.insert(couponInfo);
        if (i==1){
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean deleteById(Long id) {
        UpdateWrapper<CouponInfo> wrapper = new UpdateWrapper<>();
        wrapper.set("update_time",DateUtils.getNowDate());
        wrapper.eq("id",id);
        wrapper.set("is_deleted",1);
        int i = couponInfoMapper.update(null, wrapper);
        if (i == 1){
            return true;
        }else {
            return false;
        }
    }
}
